<?php

namespace Admin\Controllers;

class ScenarioController extends AuthController {

    public function indexAction() {
        $materialGroupModel = model('Scenario/Scenario');
        $_REQUEST['userId'] = 1;
        if ($_REQUEST['userId']) {
            $condition['createUser'] = $_REQUEST['userId'];
        }
        $condition['isDel'] = IS_DEL_NO;
        $count = $materialGroupModel->getCountNumber($condition);
        $list = $materialGroupModel->getLists($condition, $fieldArr = array(), $materialGroupModel->getPk() . " desc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val) {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $Page->show();
        $this->view->show = $show;
        $this->view->list = $list;
    }

    public function scenarioForestAction() {
        $scenarioForestModel = model('Scenario/ScenarioForest');
        if ($_REQUEST['id']) {
            $condition['scenarioId'] = $_REQUEST['id'];
        }
        $count = $scenarioForestModel->getCountNumber($condition);
        $list = $scenarioForestModel->getLists($condition, $fieldArr = array(), $scenarioForestModel->getPk() . " asc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val) {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $Page->show();
        $this->view->show = $show;
        $this->view->list = $list;
    }

    public function scenarioTreeNodeAction() {
        $scenarioForestModel = model('Scenario/ScenarioTreeNode');
        if ($_REQUEST['id']) {
            $condition['forestId'] = $_REQUEST['id'];
        }
        $count = $scenarioForestModel->getCountNumber($condition);
        $list = $scenarioForestModel->getLists($condition, $fieldArr = array(), $scenarioForestModel->getPk() . " desc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val) {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $Page->show();
        $this->view->show = $show;
        $this->view->list = $list;
    }

    public function scenarioNodeAddAction() {
        $id = $_REQUEST['id'];
        $scenarioForestModel = model('Scenario/ScenarioForest');
        $scenarioForest = $scenarioForestModel->getDataById($id);

        $scenarioRoleModel = model('Scenario/ScenarioRole');
        $condition['scenarioId'] = $scenarioForest['scenarioId'];
        $roleList = $scenarioRoleModel->getLists($condition, array(), $scenarioRoleModel->getPk() . " desc", 0, 0, "");

        unset($condition);
        $materialModel = model('Scenario/Material');
        $condition['createUser'] = 1;
        $condition['isDel'] = 0;
        $materialList = $materialModel->getLists($condition, array(), $materialModel->getPk() . " desc", 0, 0, "");
        foreach ($materialList as $key => $value) {
            if ($value['type'] == 1) {
                $imgList[] = $value;
            } else if ($value['type'] == 2) {
                $videoList[] = $value;
            } else if ($value['type'] == 3) {
                $musicList[] = $value;
            }
        }
        $this->view->imgList = $imgList;
        $this->view->videoList = $videoList;
        $this->view->musicList = $musicList;
        $this->view->roleList = $roleList;
    }

    public function ajaxScenarioNodeAddAction() {
        $id = $_POST['id'];
        $scenarioForestModel = model('Scenario/ScenarioForest');
        $scenarioForest = $scenarioForestModel->getDataById($id);
        if (empty($scenarioForest)) {
            E("剧幕不存在");
        }
        $this->db->begin();
        $scenarioNodeModel = model('Scenario/ScenarioNode');
        $condition['nodeType'] = intval($_POST['nodeType']);
        $list = $scenarioNodeModel->getLists($condition, array(), "", 0, 0, "");
        if (intval($_POST['nodeType']) == 1) {
            $name = "顺序节点" . (count($list) + 1);
        } else if (intval($_POST['nodeType']) == 2) {
            $name = "分支节点" . (count($list) + 1);
        } else if (intval($_POST['nodeType']) == 3) {
            $name = "结束节点" . (count($list) + 1);
        }if (intval($_POST['nodeType']) == 4) {
            $name = "选项节点" . (count($list) + 1);
        }
        $scenarioNodeModel->setName($name);
        $scenarioNodeModel->setNodeType(intval($_POST['nodeType']));
        $scenarioNodeModel->setType(intval($_POST['type']));
        $scenarioNodeModel->setCharacter(trim($_POST['character']));
        $scenarioNodeModel->setForegroundImgId(intval($_POST['foregroundImgId']));
        $scenarioNodeModel->setVideoId(intval($_POST['videoId']));
        $scenarioNodeModel->setMusicId(intval($_POST['musicId']));
        $scenarioNodeModel->setRoleId(intval($_POST['roleId']));
        $scenarioNodeModel->setBackgroundImgId(intval($_POST['backgroundImgId']));
        $scenarioNodeModel->setIsTriggerEnd(intval($_POST['isTriggerEnd']));
        $scenarioNodeModel->setIsReuse(intval($_POST['isReuse']));
        $scenarioNodeModel->setIsAddScore(intval($_POST['isAddScore']));
        $scenarioNodeModel->setShowType(intval($_POST['showType']));
        $scenarioNodeModel->setExtend(trim($_POST['extend']));
        $scenarioNodeModel->setScenarioId($scenarioForest['scenarioId']);
        $scenarioNodeModel->setForestId($id);
        $scenarioNodeModel->setBackgroundMusicId($_POST['backgroundMusicId']);
        $result = $scenarioNodeModel->save();
        if ($result == FALSE) {
            $messages = $scenarioNodeModel->getMessages();
            foreach ($messages as $message) {
                $this->db->rollback();
                E($message);
            }
        }
        $nodeId = $scenarioNodeModel->getId();
        $scenarioTreeNodeModel = model('Scenario/ScenarioTreeNode');
        $scenarioTreeNodeModel->setScenarioId($scenarioForest['scenarioId']);
        $scenarioTreeNodeModel->setPid(intval($_POST['pid']));
        $scenarioTreeNodeModel->setJumpId(intval($_POST['jumpId']));
        $scenarioTreeNodeModel->setForestId($id);
        $scenarioTreeNodeModel->setNodeId($nodeId);
        $result = $scenarioTreeNodeModel->save();
        if ($result == FALSE) {
            $messages = $scenarioTreeNodeModel->getMessages();
            foreach ($messages as $message) {
                $this->db->rollback();
                E($message);
            }
        }
        $this->db->commit();
        $this->ajaxReturn();
    }

    public function scenarioNodeEditAction() {
        $id = $_REQUEST['id'];
        $scenarioTreeNodeModel = model('Scenario/ScenarioTreeNode');
        $scenarioTreeNode = $scenarioTreeNodeModel->getDataById($id);

        $scenarioNodeModel = model('Scenario/ScenarioNode');
        $scenarioNode = $scenarioNodeModel->getDataById($scenarioTreeNode['nodeId']);

        $scenarioRoleModel = model('Scenario/ScenarioRole');
        $condition['scenarioId'] = $scenarioTreeNode['scenarioId'];
        $roleList = $scenarioRoleModel->getLists($condition, array(), $scenarioRoleModel->getPk() . " desc", 0, 0, "");

        unset($condition);
        $materialModel = model('Scenario/Material');
        $condition['createUser'] = 1;
        $condition['isDel'] = 0;
        $materialList = $materialModel->getLists($condition, array(), $materialModel->getPk() . " desc", 0, 0, "");
        foreach ($materialList as $key => $value) {
            if ($value['type'] == 1) {
                $imgList[] = $value;
            } else if ($value['type'] == 2) {
                $videoList[] = $value;
            } else if ($value['type'] == 3) {
                $musicList[] = $value;
            }
        }
        $this->view->imgList = $imgList;
        $this->view->videoList = $videoList;
        $this->view->musicList = $musicList;
        $this->view->roleList = $roleList;
        $this->view->scenarioTreeNode = $scenarioTreeNode;
        $this->view->scenarioNode = $scenarioNode;
    }

    public function ajaxScenarioNodeEditAction() {
        $id = $_POST['id'];
        $this->db->begin();
        $scenarioTreeNodeModel = model('Scenario/ScenarioTreeNode');
        $scenarioTreeNode = $scenarioTreeNodeModel->getDataById($id, "", true);

        if (empty($scenarioTreeNode->toArray())) {
            $this->db->rollback();
            E("剧本节点不存在");
        }
        $scenarioNodeModel = model('Scenario/ScenarioNode');
        $scenarioNode = $scenarioNodeModel->getDataById($scenarioTreeNode->getNodeId(), "", true);
        if (empty($scenarioNode->toArray())) {
            $this->db->rollback();
            E("节点不存在");
        }
        $scenarioTreeNode->setPid(intval($_POST['pid']));
        $scenarioTreeNode->setJumpId(intval($_POST['jumpId']));
        $result = $scenarioTreeNode->save();
        if ($result == FALSE) {
            $messages = $scenarioTreeNode->getMessages();
            foreach ($messages as $message) {
                $this->db->rollback();
                E($message);
            }
        }
        $scenarioNode->setNodeType(intval($_POST['nodeType']));
        $scenarioNode->setType(intval($_POST['type']));
        $scenarioNode->setCharacter(trim($_POST['character']));
        $scenarioNode->setForegroundImgId(intval($_POST['foregroundImgId']));
        $scenarioNode->setVideoId(intval($_POST['videoId']));
        $scenarioNode->setMusicId(intval($_POST['musicId']));
        $scenarioNode->setRoleId(intval($_POST['roleId']));
        $scenarioNode->setBackgroundImgId(intval($_POST['backgroundImgId']));
        $scenarioNode->setIsTriggerEnd(intval($_POST['isTriggerEnd']));
        $scenarioNode->setIsReuse(intval($_POST['isReuse']));
        $scenarioNode->setIsAddScore(intval($_POST['isAddScore']));
        $scenarioNode->setShowType(intval($_POST['showType']));
        $scenarioNode->setExtend(trim($_POST['extend']));
        $scenarioNode->setBackgroundMusicId($_POST['backgroundMusicId']);
        $result = $scenarioNode->save();
        if ($result == FALSE) {
            $messages = $scenarioNode->getMessages();
            foreach ($messages as $message) {
                $this->db->rollback();
                E($message);
            }
        }
        $this->db->commit();
        $this->ajaxReturn();
    }

    public function scenarioRoleAction() {
        $materialGroupModel = model('Scenario/ScenarioRole');
        $condition['scenarioId'] = $_REQUEST['id'];
        $count = $materialGroupModel->getCountNumber($condition);
        $list = $materialGroupModel->getLists($condition, $fieldArr = array(), $materialGroupModel->getPk() . " desc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val) {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $Page->show();
        $this->view->show = $show;
        $this->view->list = $list;
    }

    public function roleAddAction() {
        $materialModel = model('Scenario/Material');
        $condition['createUser'] = 1;
        $condition['isDel'] = 0;
        $materialList = $materialModel->getLists($condition, array(), $materialModel->getPk() . " desc", 0, 0, "");
        foreach ($materialList as $key => $value) {
            if ($value['type'] == 1) {
                $imgList[] = $value;
            } else if ($value['type'] == 2) {
                $videoList[] = $value;
            } else if ($value['type'] == 3) {
                $musicList[] = $value;
            }
        }
        $this->view->imgList = $imgList;
        $this->view->videoList = $videoList;
        $this->view->musicList = $musicList;
    }

    public function ajaxRoleAddAction() {
        $scenarioRoleModel = model('Scenario/ScenarioRole');
        $scenarioRoleModel->setImgId($_POST['imgId']);
        $scenarioRoleModel->setName($_POST['name']);
        $scenarioRoleModel->setScenarioId($_POST['scenarioId']);
        $result = $scenarioRoleModel->save();
        if ($result == FALSE) {
            $messages = $scenarioRoleModel->getMessages();
            foreach ($messages as $message) {
                $this->db->rollback();
                E($message);
            }
        }
        $this->ajaxReturn();
    }

    public function roleEditAction() {
        $id = $_REQUEST['id'];
        $scenarioRoleModel = model('Scenario/ScenarioRole');
        $scenarioRole = $scenarioRoleModel->getDataById($id);
        if (empty($scenarioRole)) {
            E("数据不存在");
        }
        $materialModel = model('Scenario/Material');
        $condition['createUser'] = 1;
        $condition['isDel'] = 0;
        $materialList = $materialModel->getLists($condition, array(), $materialModel->getPk() . " desc", 0, 0, "");
        foreach ($materialList as $key => $value) {
            if ($value['type'] == 1) {
                $imgList[] = $value;
            } else if ($value['type'] == 2) {
                $videoList[] = $value;
            } else if ($value['type'] == 3) {
                $musicList[] = $value;
            }
        }
        $this->view->role = $scenarioRole;
        $this->view->imgList = $imgList;
        $this->view->videoList = $videoList;
        $this->view->musicList = $musicList;
    }

    public function ajaxRoleEditAction() {
        $scenarioRoleModel = model('Scenario/ScenarioRole');
        $scenarioRole = $scenarioRoleModel->getDataById($_POST['id'], "", true);
        if (empty($scenarioRole->toArray())) {
            E("数据不存在");
        }
        $scenarioRole->setImgId($_POST['imgId']);
        $scenarioRole->setName($_POST['name']);
        $result = $scenarioRole->save();
        if ($result == FALSE) {
            $messages = $scenarioRole->getMessages();
            foreach ($messages as $message) {
                $this->db->rollback();
                E($message);
            }
        }
        $this->ajaxReturn();
    }

}
